package com.example.graduationdesign.controller;

import com.example.graduationdesign.algorithm.WordRecommendationSystem;
import com.example.graduationdesign.pojo.dao.*;
import com.example.graduationdesign.pojo.dto.*;
import com.example.graduationdesign.pojo.vo.*;
import com.example.graduationdesign.service.ConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    public ConsumerService consumerService;


    /**
     * 获取用户在所处阶段所需要学习的英语单词个数
     * @param userStage
     * @return
     */
    @PostMapping("/word")
    public Result<List<WordDao>> getWord(@RequestBody UserStageDao userStage) {
        List<WordDao> wordDaos = consumerService.wordinfo(Integer.parseInt(userStage.getUserStage()));
        return Result.success(wordDaos);
    }

    /**
     * 修改用户在学习状态下的状态情况
     * @param userStatusStudyDao
     * @return
     */

    @PostMapping("/status/study")
    public Result<Void> updateStatus(@RequestBody UserStatusStudyDao userStatusStudyDao){
        System.out.println("11111111111111111111111111111111111111");
        System.out.println(userStatusStudyDao);
        consumerService.UpadateStudyStatus(userStatusStudyDao);
        return Result.success();
    }


    /**
     * 用于获取用户已经完成的学习记录
     * @param studyRecordDto
     * @return
     */
    @PostMapping("/status/getStatus")
    public Result<UserStatusStudyDao> getStatus(@RequestBody StudyRecordDto studyRecordDto){
        UserStatusStudyDao userStatusStudyDao = consumerService.GetStatus(studyRecordDto);
        System.out.println(userStatusStudyDao);
        return Result.success(userStatusStudyDao);
    }

    /**
     *
     * @param likeOrFavoriteDao
     * @return
     */
    @PostMapping("/status/likeorfavorite")
    public Result<Void> isLikeOrFavorite(@RequestBody LikeOrFavoriteDao likeOrFavoriteDao){
        consumerService.isLikeOrFavorite(likeOrFavoriteDao);
        return Result.success();
    }


    /**
     * 用于用户实现获取打卡题目的接口
     * @param request
     * @return
     */

    @PostMapping("/question/check")
    public Result<List<QuestionCheckDao>> GetCheckQuestion(@RequestBody Map<String,String> request){
        Integer consumerStage = Integer.parseInt(request.get("consumerStage"));
        Long consumerId = Long.parseLong(request.get("consumerId"));
        System.out.println(consumerId+" "+consumerStage);
        List<QuestionCheckDao> questionCheckDao = consumerService.GetCheckQuestion(consumerStage,consumerId);
        return Result.success(questionCheckDao);
    }


    /**
     * 用户将用户前端所进行打卡的数据向后端传送，并将其插入进数据库中
     * @param questionCheckDto
     * @return
     */
    @PostMapping("/question/is_check")
    public Result<Void> GetIsCheckQuestion(@RequestBody QuestionCheckDto questionCheckDto){
        System.out.println("1111111111111111111111111111111111111111");
        System.out.println(questionCheckDto);
        consumerService.WriteCheckRecord(questionCheckDto);
        return Result.success();
    }


    /**
     * 用于获取该用户今日是否打卡的数据
     * @param request
     * @return
     */
    @PostMapping("/question/is_check_date")
    public Result<Integer> GetIsChecked(@RequestBody Map<String ,String> request){
        Integer is_True = consumerService.GetIsChecked(Long.parseLong(request.get("consumerId")));
        System.out.println(is_True);
        return Result.success(is_True);
    }


    /**
     * 获取某用户的全部打卡记录，并将其返回至前端
     * @param request
     * @return
     */
    @PostMapping("/question/checkrecords")
    public Result<List<CheckRecordDao>> GetCheckRecords(@RequestBody Map<String ,String> request){
        List<CheckRecordDao> checkRecordDaos = consumerService.GetCheckRecords(Long.parseLong(request.get("consumerId")));
        return Result.success(checkRecordDaos);
    }


    /**
     * 用于删除用户当日的所有打卡记录
     * @param checkRecordDao
     * @return
     */
    @PostMapping("/checkrecords/delete")
    public Result<Void> DeleteCheckRecords(@RequestBody CheckRecordDao checkRecordDao){
        consumerService.DeleteCheckRecords(checkRecordDao);
        return Result.success();
    }


    /**
     * 用于获取某阶段用户的测试数据
     * @param request
     * @return
     */
    @PostMapping("/test/getQuestion")
    public Result<List<WordDao>> GetTestQuestion(@RequestBody Map<String,String> request){
        WordRecommendationSystem wordRecommendationSystem = new WordRecommendationSystem();
        List<WordDao> wordDaos = wordRecommendationSystem.getRecommendedWords(Long.parseLong(request.get("userId")),Integer.parseInt(request.get("count")));
        return Result.success(wordDaos);
    }


    /**
     * 用于动态获取用户应该学习的单词数据
     * @param data
     * @return
     */
    @PostMapping("/get_word_number")
    public Result<Integer> GetWordNumber(@RequestBody Integer data){
        Integer WordNumber = consumerService.GetWordNumber(data);
        return Result.success(WordNumber);
    }


    /**
     * 用于获取用户在某个阶段已经熟悉的单词总数
     * @param request
     * @return
     */
    @PostMapping("/get_familar_number")
    public Result<Integer> GetFamilarNumber(@RequestBody Map<String,String> request){

        Integer FamilarNumber = consumerService.GetFamilarNumber(Long.parseLong(request.get("consumerId")),Integer.parseInt(request.get("consumerStage")));
        return Result.success(FamilarNumber);
    }

    /**
     * 用于获取用户喜欢的单词数量
     * @param request
     * @return
     */
    @PostMapping("/get_like_number")
    public Result<Integer> GetLikeNumber(@RequestBody Map<String,String> request){

        Integer LikeNumber = consumerService.GetLikeNumber(Long.parseLong(request.get("consumerId")),Integer.parseInt(request.get("consumerStage")));
        System.out.println(LikeNumber);
        return Result.success(LikeNumber);
    }

    /**
     * 用于获取用户收藏的单词数量
     * @param request
     * @return
     */
    @PostMapping("/get_favorite_number")
    public Result<Integer> GetFavoriteNumber(@RequestBody Map<String,String> request){
        System.out.println(request.get("consumerId")+request.get("consumerStage"));
        Integer FavoriteNumber = consumerService.GetFavoriteNumber(Long.parseLong(request.get("consumerId")),Integer.parseInt(request.get("consumerStage")));
        System.out.println(FavoriteNumber);
        return Result.success(FavoriteNumber);
    }


    /**
     * 增加用户对文章的阅读量
     * @param request
     * @return
     */
    @PostMapping("/set_read_number")
    public Result<Void> SetArticleReadNumber(@RequestBody Map<String,String> request){
        System.out.println(request.get("duration")+" "+request.get("articleId"));
        if(Integer.parseInt(request.get("duration")) == 5000){
            consumerService.SetArticleReadNumber(Long.parseLong(request.get("articleId")));
            return Result.success();
        }

        return null;
    }


    /**
     * 用于获取用户需要默写的单词
     * @param request
     * @return
     */
    @PostMapping("/get_recite_word")
    public Result<List<WordDao>> GetReciteWord(@RequestBody Map<String,String> request){
       Long userId = Long.parseLong(request.get("userId"));

       Integer userStage = Integer.parseInt(request.get("userStage"));

       List<WordDao> wordDaos = consumerService.GetReciteWord(userId,userStage);

        return Result.success(wordDaos);
    }


    /**
     * 用于用户获取积分大小
     * @param request
     * @return
     */
    @PostMapping("/score")
    public Result<Long> GetScore(@RequestBody Map<String,String> request){
        Long userId = Long.parseLong(request.get("consumerId"));

        return Result.success(consumerService.GetScore(userId));
    }


    /**
     * 用于实现获取用户读书笔记的接口实现
     * @param request
     * @return
     */
    @PostMapping("/note_book")
    public Result<List<NoteBookDao>> GetNoteBook(@RequestBody Map<String,String> request){
        Long userId = Long.parseLong(request.get("consumerId"));

        return Result.success(consumerService.GetNoteBook(userId));
    }

    /**
     * 实现用户增加读书笔记的接口
     * @param request
     * @return
     */
    @PostMapping("/note_book/add")
    public Result<Void> AddNoteBook(@RequestBody Map<String,String> request){
        Long userId = Long.parseLong(request.get("consumerId"));
        String content = request.get("content");

        String title = request.get("title");
        consumerService.AddNoteBook(userId,title,content);
        return Result.success();
    }

    /**
     * 实现用户修改笔记的功能
     * @param request
     * @return
     */
    @PostMapping("/note_book/update")
    public Result<Void> UpdateNoteBook(@RequestBody Map<String,String> request){
        Long userId = Long.parseLong(request.get("consumerId"));
        String content = request.get("noteContent");
        Long code = Long.parseLong(request.get("noteId"));
        String title = request.get("noteTitle");
        consumerService.UpdateNoteBook(userId,code,content,title);
        return Result.success();
    }

    /**
     * 用于实现用户删除笔记的操作
     * @param request
     * @return
     */
    @PostMapping("/note_book/delete")
    public Result<Void> DeleteNoteBook(@RequestBody Map<String,String> request){
        System.out.println(request.get("noteId"));
        Long noteCode = Long.parseLong(request.get("noteId"));
        consumerService.DeleteNoteBook(noteCode);
        return Result.success();
    }


    /**
     * 用于获取用户的排名
     * @param request
     * @return
     */
    @PostMapping("/get_rank")
    public Result<Long> GetRank(@RequestBody Map<String,String> request){

        Long userID = Long.parseLong(request.get("consumerId"));
        Integer userStage = Integer.parseInt(request.get("consumerStage"));
        Long rank = consumerService.GetRank(userID,userStage);
        return Result.success(rank);
    }


    /**
     * 用于实现对词库中的内容进行获取操作
     * @param request
     * @return
     */
    @PostMapping("/get_all_words")
    public Result<List<WordPersonalVo>> GetAllWords(@RequestBody Map<String,String> request){

        Long userID = Long.parseLong(request.get("userId"));
        Integer userStage = Integer.parseInt(request.get("userStage"));
        List<WordPersonalVo> wordDaos = consumerService.GetAllWords(userID,userStage);
        return Result.success(wordDaos);
    }


    /**
     * 用于获取用户信息方向的轮播图数据
     * @return
     */
    @GetMapping("/articles/banners")

    public Result<List<BannersDto>> GetBanners(){
        List<BannersDto> bannersDtos = consumerService.GetBanners();
        return Result.success(bannersDtos);
    }


    /**
     * 用于获取用户的积分排名前十
     * @return
     */
    @PostMapping("/rank")

    public Result<List<ConsumerScoreDto>> GetConsumerScoreRank(@RequestBody Map<String,String> request){

        List<ConsumerScoreDto> consumerScoreDtos = consumerService.GetConsumerScoreRank(Integer.parseInt(request.get("stage")));
        System.out.println("---------------------------");
        System.out.println(consumerScoreDtos);
        return Result.success(consumerScoreDtos);
    }


    /**
     * 用于获取用户在某一个阶段的学习进度排名
     * @param request
     * @return
     */
    @PostMapping("/progress/rank")
    public Result<List<ProgressRankDto>> GetProgressRank(@RequestBody Map<String,String> request){
        List<ProgressRankDto> progressRankDtos = consumerService.GetProgressRank(Integer.parseInt(request.get("stage")));
        System.out.println("---------------------------");
        System.out.println(progressRankDtos);
        return Result.success(progressRankDtos);
    }


    /**
     * 用于获取用户在某一个阶段的学习进度数据
     * @param request
     * @return
     */
    @PostMapping("/progress/personal")
    public Result<MyStudyProgressDto> GetData(@RequestBody Map<String,String> request){
        MyStudyProgressDto myStudyProgressDto = consumerService.getPersonalProgress(Long.parseLong(request.get("userId")),Integer.parseInt(request.get("userStage")));
        System.out.println("---------------------------");
        System.out.println(myStudyProgressDto);
        return Result.success(myStudyProgressDto);
    }


    /**
     * 用于实现更新用户闯关的实时数据
     * @param request
     * @return
     */
    @PostMapping("/update_level")
    public Result<Void> SetUserLevel(@RequestBody Map<String,String> request){
       consumerService.SetUserLevel(Long.parseLong(request.get("userId")),Integer.parseInt(request.get("newLevel")));

       return Result.success();
    }


    /**
     * 用于分页获取用户在某个阶段所需要学习的单词数量
     * @param request
     * @return
     */
    @PostMapping("/word/page")
    public Result<PageInfo<WordDao>> GetWord(@RequestBody Map<String,String> request){
        return Result.success(consumerService.GetWord(Long.parseLong(request.get("userId")),Integer.parseInt(request.get("userStage")),Integer.parseInt(request.get("pageNum")),Integer.parseInt(request.get("pageSize"))));
    }


    /**
     * 主要用于实现将用户测试数据往数据库中插入
     * @param submitTestDto
     * @return
     */
    @PostMapping("/test/submit")
    public Result<Long> SubmitTest(@RequestBody SubmitTestDto submitTestDto){
        System.out.println(submitTestDto);
        Long score = consumerService.SubmitTest(submitTestDto);
        return Result.success(score);
    }


    /**
     * 主要获取某用户在某阶段的打卡的错误题目
     * @param requetst
     * @return
     */
    @PostMapping("/mistakes/daka")
    public Result<List<CheckMistakesVo>> GetCheckMistakes(@RequestBody Map<String,String> requetst){
        Long userId = Long.parseLong(requetst.get("userId"));
        Integer userStage = Integer.parseInt(requetst.get("userStage"));
        return Result.success(consumerService.GetCheckMistakes(userId,userStage));
    }


    /**
     * 主要用于获取某用户在某阶段的测试错误记录
     * @param requetst
     * @return
     */
    @PostMapping("/mistakes/test")
    public Result<List<TestMistakesVo>> GetTestMistakes(@RequestBody Map<String,String> requetst){
        Long userId = Long.parseLong(requetst.get("userId"));
        Integer userStage = Integer.parseInt(requetst.get("userStage"));
        return Result.success(consumerService.GetTestMistakes(userId,userStage));
    }


    /**
     * 用于获取某个单词的详细信息
     * @param request
     * @return
     */
    @PostMapping("/word/detail")
    public Result<WordDao> GetWordDetail(@RequestBody Map<String,String> request){
        Long wordCode = Long.parseLong(request.get("wordCode"));

        return Result.success(consumerService.GetWordDetail(wordCode));
    }


    /**
     * 用于获取用户已掌握的数量
     * @param request
     * @return
     */
    @PostMapping("/mastered/count")
    public Result<Long> GetMasterCount(@RequestBody Map<String,String> request){

        Long userId = Long.parseLong(request.get("userId"));
        Integer userStage = Integer.parseInt(request.get("userStage"));
        if(request.get("source").equals("checkrecord")){
            return Result.success(consumerService.GetCheckMasterCount(userId,userStage));
        }
        if(request.get("source").equals("testrecord")){
            return Result.success(consumerService.GetTestMasterCount(userId,userStage));
        }
        return null;
    }


    /**
     * 主要用于帮助用户实现对错误单词的重新掌握和学习
     * @param request
     * @return
     */
    @PostMapping("/mistakes/mastered")
    public Result<Void> UpdateMistakesMastered(@RequestBody Map<String,String> request){

        Long userId = Long.parseLong(request.get("userId"));
        Long wordCode = Long.parseLong(request.get("wordCode"));
        String time = request.get("time");
        System.out.println(userId+" "+wordCode+" "+time);
        System.out.println(request.get("source"));
        if(request.get("source").equals("daka")){
            return Result.success(consumerService.UpdateCheckMistakesMastered(userId,wordCode,time));
        }
        if(request.get("source").equals("test")){
            return Result.success(consumerService.UpdateTestMistakesMastered(userId,wordCode,time));
        }
        return null;
    }




    // 获取默写章节列表
    @PostMapping("/get_recite_chapters")
    public Result getReciteChapters(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        Integer userStage = Integer.parseInt(request.get("userStage").toString());

        List<Map<String, Object>> chapters = consumerService.getReciteChapters(userId, userStage);
        return Result.success(chapters);
    }


    // 获取指定章节的单词
    @PostMapping("/get_recite_words")
    public Result<List<WordDao>> getReciteWords(@RequestBody Map<String, Object> params) {
        Long userId = Long.parseLong(params.get("userId").toString());
        Integer userStage = Integer.parseInt(params.get("userStage").toString());
        Integer chapterIndex = Integer.parseInt(params.get("chapterIndex").toString());

        List<WordDao> words = consumerService.getReciteWords(userId, userStage, chapterIndex);
        return Result.success(words);
    }


    /**
     * 用于实现将用户的默写数据写入后端数据库中
     * @param reciteRecordDto
     * @return
     */
    @PostMapping("/save-record")
    public Result<Long> saveReciteRecord(@RequestBody ReciteRecordDto reciteRecordDto) {
        System.out.println(reciteRecordDto);
        try {
            Long recordId = consumerService.saveReciteRecord(reciteRecordDto);
            return Result.success(recordId);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 用于判断用户是否在当前阶段完成了默写操作
     * @param params
     * @return
     */
    @PostMapping("/check_chapter_completion")
    public Result<IsCompleteVo> checkChapterCompletion(@RequestBody Map<String, Object> params) {
        System.out.println("你麻痹");
        Long userId = Long.parseLong(params.get("userId").toString());

        Integer userStage = Integer.parseInt(params.get("userStage").toString());
        Integer chapterIndex = Integer.parseInt(params.get("chapterIndex").toString());

        IsCompleteVo isCompleteVo= consumerService.checkChapterCompletion(userId, userStage, chapterIndex);
        return Result.success(isCompleteVo);
    }



    // 获取默写报告
    @PostMapping("/report")
    public Result<WriteReportVo> getReciteReport(@RequestBody Map<String,String > request) {
        Long userId = Long.parseLong(request.get("userId"));

        Long recorId = Long.parseLong(request.get("recordId"));

        return Result.success(consumerService.getReciteReport(userId,recorId));
    }



    // 获取默写详情
    @PostMapping("/report/details")
    public Result<List<WriteReportDetailVo>> getReciteDetails(@RequestBody Map<String ,String> request) {
        Long recordId = Long.parseLong(request.get("recordId"));
        return Result.success(consumerService.getReciteDetails(recordId));
    }

    //实现用户进行重新默写的操作
    @PostMapping("/report/delete")
    public Result<Void> DeleteData(@RequestBody Map<String ,String> request) {
        consumerService.DeleteData(Long.parseLong(request.get("recordId")));
        return Result.success();
    }


    /**
     * 用于实现用户利用积分兑换VIP的功能
     * @param request
     * @return
     */
    @PostMapping("/exchange_vip")
    public Result<Void> ExchangeVip(@RequestBody Map<String ,String> request) {
        consumerService.ExchangeVip(Long.parseLong(request.get("consumerId")));
        return Result.success();
    }

}
